package com.bruce.main.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.bruce.main.BR;
import com.bruce.main.R;
import com.bruce.main.databinding.ActMainBinding;
import com.lks.annotation.Module;

import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.RxPermissionsUtils;

@Module(value = "main_ActMain")
public class ActMain extends BaseActivity<ActMainBinding,ViewModel> {

    private int mYear;
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.act_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        getPermission();
        Log.e("bruce",binding.calendarView.getSelectedCalendar().getYear()+"年"+binding.calendarView.getSelectedCalendar().getMonth()+"月"+binding.calendarView.getSelectedCalendar().getDay()+"日");
        viewModel.init(binding.calendarView.getSelectedCalendar());
        mYear=binding.calendarView.getCurYear();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.changeCalendarView.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                if (!binding.calendarLayout.isExpand()) {
                    binding.calendarLayout.expand();
                    return;
                }
                binding.tvYear.setVisibility(View.GONE);
                binding.tvLunar.setVisibility(View.GONE);
                binding.calendarView.showYearSelectLayout(mYear);
                binding.tvMonthDay.setText(String.valueOf(mYear));
            }
        });
        viewModel.uc.changeSelectDate.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                binding.tvYear.setVisibility(View.VISIBLE);
                binding.tvLunar.setVisibility(View.VISIBLE);
              viewModel.changeCalendar(binding.calendarView.getSelectedCalendar());
            }
        });
        viewModel.uc.refreshDone.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                binding.smartRefreshLayout.finishLoadMore();
                binding.smartRefreshLayout.finishRefresh();
            }
        });
    }
    private void getPermission() {
        try {
            String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                  };
            RxPermissionsUtils.getInstance().applyPermission(this).request(permissions).subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean aBoolean) throws Exception {
                }
            });


        } catch (Exception e) {
            Log.e("Exception", e.getMessage());
        }
    }
}